package com.app.service;

import com.app.model.main.ClickRecord;
import com.app.model.response.RetStatus;
import com.app.repository.interfaces.ClickRecordMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * Created by Lichaojie on 2017/7/17. It's a beautiful day.
 */
@Service
public class ClickService {

    @Autowired
    private ResponseService responseService;
    @Autowired
    private ClickRecordMapper clickRecordMapper;

    public RetStatus addRecord(String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap = null;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int userId = (int)paraMap.get("userId");
        int page = (int)paraMap.get("page");
        String time = (String)paraMap.get("time");

        ClickRecord clickRecord = new ClickRecord();
        clickRecord.setUserId((long)userId)
                .setPage((byte)page)
                .setTime(time);

        if(clickRecordMapper.insertSelective(clickRecord) == 1){
            return responseService.doResponse(100);
        }else {
            return responseService.doResponse(200);
        }
    }
}
